package com.mdxx.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by MD-1 on 2017/8/2.
 */
public class MyShiroRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("授权啦");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<String> permissionSet = new HashSet<>();
        Set<String> roleSet = new HashSet<>();
        roleSet.add("10001");
        permissionSet.add("member:add");
        permissionSet.add("member:delete");
        info.setRoles(roleSet);
        info.setStringPermissions(permissionSet);
        System.out.println("###【获取角色成功】[SessionId] => {}"+SecurityUtils.getSubject().getSession().getId());
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        String username = (String) token.getPrincipal();
        UsernamePasswordToken token1 = (UsernamePasswordToken) token;
        System.out.println("user " + token1.getUsername());
        /*System.out.println("user " + username);
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
                "admin",
                "admin",
                ByteSource.Util.bytes("admin"),
                "admin");
        //查出是否有此用户*/
        return new SimpleAuthenticationInfo( "admin",
                "admin", getName());
    }
}
